package roby.privateEncryption;

import java.util.ArrayList;

public class PrivateEncryptor {
	
	private byte[] key;
	
	public PrivateEncryptor(byte[] key){
		
		this.key = key;
		
	}
	
	public String encrypt(String plainText){
		System.out.println("Original plain text: " + plainText);
		String cipherText = null;
		int size = plainText.length();
		ArrayList<String> stringList = new ArrayList<String>();
		if(size > 1024){
			for(int i = 0; i < size; i+= 1024){
				stringList.add(plainText.substring(i, i+1024));
			}
		}
		else{
			stringList.add(plainText);
		}
		byte[] encryptedMessage = new byte[size];
		int index = 0;
		for(int i = 0; i < stringList.size(); i++){
			byte[] string = stringList.get(i).getBytes();
			for(int j = 0; j < size -2 ; j++){
				
				encryptedMessage[j] = (byte) (key[index++%1024] ^ string[j]);
				while((char)encryptedMessage[j] == '\r' || (char)encryptedMessage[j] == '\n')
					encryptedMessage[j] = (byte) (key[index++%1024] ^ string[j]);
				
			}
			encryptedMessage[size-2] = '\r'; 
			encryptedMessage[size-1] = '\n';
		}
		cipherText = new String(encryptedMessage);
		System.out.println("Encrypted Message: " + cipherText);
		return cipherText;
		
	}

}
